<?php

    class LeproBJGroup {
    
        public function __construct($groupId, $groupCode) {
            $this->_id = $groupId;
            $this->_code = $groupCode;
            $this->_usersIds = array();
            $this->_scoreSum = 0;
            $this->_cTime = null;
        }
        
        public function addUser($user) {
            // if ($user instanceof LeproUser) {
            $userId = $user->getID();
            if (!in_array($userId, $this->_usersIds)) {                
                $this->_usersIds[] = $userId;
                $user->tieWithGroup($this);
                /* if ($newGroupScore != null) {
                    $this->_scoreSum = $newGroupScore;
                } else {
                    $score = $this->getExactCardScoreForGroup($user->getCardId());
                    $this->addToScoreSum($score);
                } */
                // TODO: required to call updateScoreSum for group and updateGScore for user
                //      (transfer calculating interfaces to the method?) 
            }
        }
        
        /* public function addUsers($usersIdsArray) {
            foreach($usersIdsArray as $userId) {
                $this->addUser($userId);
            }
        } */
        
        public function assignUsers($usersIds) {
            // call only from DAO
            $this->_usersIds = $usersIds;
        }  
        
        public function removeUser($user) {
            $userId = $user->getID();
            $key = array_search($userId, $this->_usersIds);
            if ($key === false) return;
            $user->leaveGroup($this);
            unset($this->_usersIds[$key]);
            /* if ($newGroupScore != null) {
                $this->_scoreSum = $newGroupScore;
            } else {
                $score = $this->getExactCardScoreForGroup($user->getCardId());
                $this->subtractFromScoreSum($score);
            } */
            // TODO: required to call updateScoreSum for group and updateGScore for user
            //      (transfer calculating interfaces to the method?)
        }
        
        public function haveUser($userId) {
            return (array_search($userId, $this->_usersIds) !== false);
        }
        
        public function getID() {
            return $this->_id;
        }
        
        public function getCode() {
            return $this->_code;
        }
        
        public function getUsersIDs() {
            //return array_values($this->_usersIds);
            return $this->_usersIds;
        }
        
        public function getUsersCount() {
            return count($this->_usersIds);
        }        
        
        public function getScoreSum() {
            return $this->_scoreSum;
        } 
        
        public function setScoreSum($scoreSum) { // for DAO
            $this->_scoreSum = $scoreSum;
        }
        
        public function updateScoreSum($scoreSum) { // for controller
            $this->_scoreSum = $scoreSum;
        }
        
        public function getCreationTime() {
            return $this->_cTime;
        }
        
        public function setCreationTime($cTime) { // for DAO
            $this->_cTime = $cTime;
        }
        
        public function setTimeCreated($cTime) { // for controller
            $this->_cTime = $cTime;
        }
        
        public function getValue() {
            return self::getSumValue($this->_scoreSum);
            // if ($this->_scoreSum < 21) return $this->_scoreSum * 0.02;
            // if ($this->_scoreSum > 21) return -2;
            // if ($this->_scoreSum == 21) return 1;
        } 
        
        public static function getSumValue($scoreSum) {
            if ($scoreSum < 21) return $scoreSum * 0.02;
            if ($scoreSum > 21) return -2;
            if ($scoreSum == 21) return 1;            
        }
        
        protected function addToScoreSum($score) {
            $this->_scoreSum += $scoreSum; 
        }
        
        protected function subtractFromScoreSum($score) {
            $this->_scoreSum -= $scoreSum; 
        }
        
        public function isProtected() {
            return ($this->getCode() != '');
        }
        
        public static function calculateGroupsSumValue($scoresList) {
            if (count($scoresList) == 0) return 0;
            $valuesSum = 0;
            foreach ($scoresList as $score) {
                $valuesSum += self::getSumValue($score);
            }
            return $valuesSum;
        }
        
        public function getSummary() {
            $usersIDs = $this->getUsersIDs();
            $haveUsers = $this->getUsersCount() > 0;
            return array(
                    "name" => $this->getID(),
                    "score" => $this->getScoreSum(),
                    "creator" => ($haveUsers ? $usersIDs[0] : ''),
                    "isprotected" => $this->isProtected(),
                    "users" => $haveUsers ? $this->getUsersIDs() : '',
                    /* ajoin, aleave */
                    "ucount" => $this->getUsersCount(),
                    "ctime" => $this->getCreationTime()
                );
        }
        
    }

?>
